unit PesquisaVendaNotaFiscal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UFormPesquisa, DB, DBTables, StdCtrls, Grids, DBGrids, Buttons,
  ExtCtrls, ToolWin, ActnMan, ActnCtrls, ActnMenus, DBCtrls, Mask, ToolEdit,
  CheckLst;

type
  TFPesquisaVendaNotaFiscal = class(TFormPesquisa)
    ECodigo: TEdit;
    Label1: TLabel;
    Label5: TLabel;
    DBLCBCliente: TDBLookupComboBox;
    DSCliente: TDataSource;
    RGSituacao: TRadioGroup;
    ENumeroNota: TEdit;
    ESerieNota: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    ENumeroSelo: TEdit;
    ESerieSelo: TEdit;
    Label4: TLabel;
    Label6: TLabel;
    Label11: TLabel;
    DBLCBTipoVenda: TDBLookupComboBox;
    DEDataEmissaoINI: TDateEdit;
    DEDataEmissaoFIM: TDateEdit;
    Label7: TLabel;
    Label8: TLabel;
    DSTipoVenda: TDataSource;
    EPedido: TEdit;
    ERomaneio: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    CkBTodasEmpresas: TCheckBox;
    Label12: TLabel;
    CLBEmpresas: TCheckListBox;
    procedure ECodigoKeyPress(Sender: TObject; var Key: Char);
    procedure DBLCBClienteKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnSelecionarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnConsultarClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CkBTodasEmpresasClick(Sender: TObject);
    procedure BtnLimparClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
   tela :String;
  end;

var
  FPesquisaVendaNotaFiscal: TFPesquisaVendaNotaFiscal;
  S :String;

implementation

uses UDmGestao, VendaNotaFiscal, UGeral, VendaNotaFiscalServico;

{$R *.dfm}

procedure TFPesquisaVendaNotaFiscal.ECodigoKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
 if Not(key in ['0'..'9',#8]) then
  key:=#0;
end;

procedure TFPesquisaVendaNotaFiscal.DBLCBClienteKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
 case key of
  116 : begin
           TDBLookupComboBox(Sender).ListSource.DataSet.Close;
           TDBLookupComboBox(Sender).ListSource.DataSet.Open;
          end ;
 end;


 if key = 46 then
  TDBLookupComboBox(Sender).KeyValue:=null;

end;

procedure TFPesquisaVendaNotaFiscal.BtnSelecionarClick(Sender: TObject);
begin
  inherited;
 if tela ='NOTAFISCALVENDA' then
  begin
   FVendaNotaFiscal.QNotaFiscal.Close;
   FVendaNotaFiscal.QNotaFiscal.Open;
   FVendaNotaFiscal.QNotaFiscal.Locate('NF_Codigo',QPesquisa.fieldByname('NF_Codigo').AsString,[loCaseInsensitive]);
   
  end
 else
  if tela ='NOTAFISCALSERVICOVENDA' then
   BEGIN
    FVendaNotaFiscalServico.QNotaFiscal.Close;
    FVendaNotaFiscalServico.QNotaFiscal.Open;
    FVendaNotaFiscalServico.QNotaFiscal.Locate('NF_Codigo',QPesquisa.fieldByname('NF_Codigo').AsString,[loCaseInsensitive]);
    FVendaNotaFiscalServico.Busca;
   end;
 Self.Close;

end;

procedure TFPesquisaVendaNotaFiscal.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
 if tela ='NOTAFISCALVENDA' then
  begin
   if FVendaNotaFiscal <> nil then
    begin
     FVendaNotaFiscal.Enabled:=true;
     FVendaNotaFiscal.Show;
    end;
  end
 else
  if tela ='NOTAFISCALSERVICOVENDA' then
   BEGIN
    if FVendaNotaFiscalServico <> nil then
     begin
      FVendaNotaFiscalServico.Enabled:=true;
      FVendaNotaFiscalServico.Show;
     end;
   end;

 FPesquisaVendaNotaFiscal:=nil;
 inherited;

end;

procedure TFPesquisaVendaNotaFiscal.BtnConsultarClick(Sender: TObject);
begin
 if (EmpresasDesmarcadas(CLBEmpresas) = CLBEmpresas.Items.Count) then
  CkBTodasEmpresasClick(Sender);
 S:=Concatenaempresas(CLBEmpresas);
 QPesquisa.Close;
 QPesquisa.SQL.Clear;
 QPesquisa.SQL.Add('Select NF_Codigo,NF_NUMERONF,NF_SERIENF,NF_NUMEROSELO,NF_SERIESELO,NF_DATAEMISSAO,NF_PEDIDOVENDA,NF_ROMANEIO,TIPVEND_DESCRICAO,CLI_RAZAO ');
 QPesquisa.SQL.Add('From NotaFiscalVenda Left join TipoVenda on NF_TIPOVENDA = TipVend_Codigo Left Join Cliente on NF_CLIENTE = Cli_Codigo ');
 QPesquisa.SQL.Add(' Where (CodigoEmpresa in ( ' + s + ') ) ');

 if RGSituacao.ItemIndex <> 3 then
  begin
   QPesquisa.SQL.Add(' and (NF_SITUACAO = :Sit )' );
   QPesquisa.ParamByName('Sit').AsInteger:=RGSituacao.ItemIndex;
  end;

 if ECodigo.Text <> '' then
  begin
   QPesquisa.SQL.Add(' and (NF_Codigo = :Cod )' );
   QPesquisa.ParamByName('Cod').AsString:=ECodigo.Text;
  end;

 if ENumeroNota.Text <> '' then
  begin
   QPesquisa.SQL.Add(' and (NF_NUMERONF = :NumeroNF )' );
   QPesquisa.ParamByName('NumeroNF').AsString:=ENumeroNota.Text;
  end;

 if ESerieNota.Text <> '' then
  begin
   QPesquisa.SQL.Add(' and (NF_SERIENF = :SerieNF )' );
   QPesquisa.ParamByName('SerieNF').AsString:=ESerieNota.Text;
  end;


 if ENumeroSelo.Text <> '' then
  begin
   QPesquisa.SQL.Add(' and (NF_NUMEROSELO = :NumeroSelo )' );
   QPesquisa.ParamByName('NumeroSelo').AsString:=ENumeroSelo.Text;
  end;

 if ESerieSelo.Text <> '' then
  begin
   QPesquisa.SQL.Add(' and (NF_SERIESELO = :SerieSelo )' );
   QPesquisa.ParamByName('SerieSelo').AsString:=ESerieSelo.Text;
  end;

 if (DEDataEmissaoINI.Date <> 0) and (DEDataEmissaoFIM.Date <> 0) then
  begin
   QPesquisa.SQL.Add(' and (NF_DATAEMISSAO  between :DataI and :DataF ) ');
   QPesquisa.ParamByName('DataI').AsDate:=DEDataEmissaoINI.Date;
   QPesquisa.ParamByName('DataF').AsDate:=DEDataEmissaoFIM.Date;
  end;

 if EPedido.Text <> '' then
  begin
   QPesquisa.SQL.Add(' and (NF_PEDIDOVENDA = :Ped )' );
   QPesquisa.ParamByName('Ped').AsString:=EPedido.Text;
  end;

 if ERomaneio.Text <> '' then
  begin
   QPesquisa.SQL.Add(' and (NF_ROMANEIO = :Rom )' );
   QPesquisa.ParamByName('Rom').AsString:=ERomaneio.Text;
  end;
 QPesquisa.SQL.Add(' Order By NF_Codigo ');
 QPesquisa.Open;
 inherited;
 BtnImprimir.Enabled:=false;
end;

procedure TFPesquisaVendaNotaFiscal.FormCreate(Sender: TObject);
begin
  inherited;
 QPesquisa.Close;
 QPesquisa.SQL.Clear;
 QPesquisa.SQL.Add('Select Emp_Codigo,Emp_Fantasia from empresa');
 QPesquisa.Open;
 QPesquisa.First;
 CLBEmpresas.Items.Clear;
 while not(QPesquisa.Eof) do
 begin
  CLBEmpresas.Items.Add(QPesquisa.fieldByname('Emp_Codigo').AsString + '-' + QPesquisa.fieldByname('Emp_Fantasia').AsString );
  QPesquisa.Next;
 end;
 CkBTodasEmpresasClick(Sender);
 QPesquisa.Close;

end;

procedure TFPesquisaVendaNotaFiscal.CkBTodasEmpresasClick(Sender: TObject);
Var
 i : Integer;
begin
  inherited;
 for i := 0 to CLBEmpresas.Items.Count - 1 do
  begin
   if StrToInt(Copy(CLBEmpresas.Items.Strings[i],1,pos('-',CLBEmpresas.Items.Strings[i])- 1)) = Codigoempresa then
    CLBEmpresas.Checked[i]:=true
   else
    CLBEmpresas.Checked[i]:=CkBTodasEmpresas.Checked;
  end;

end;

procedure TFPesquisaVendaNotaFiscal.BtnLimparClick(Sender: TObject);
begin
  inherited;
 RGSituacao.ItemIndex:=3;
 ECodigo.Text:='';
 ENumeroNota.Text:='';
 ESerieNota.Text:='';
 ENumeroSelo.Text:='';
 ESerieSelo.Text:='';
 DEDataEmissaoINI.Date:=0;
 DEDataEmissaoFIM.Date:=0;
 DBLCBTipoVenda.KeyValue:=null;
 DBLCBCliente.KeyValue:=null;
 EPedido.Text:='';
 ERomaneio.Text:='';
end;

end.
